
package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;

import com.hlingsoft.kkh.entity.*;
import com.hlingsoft.kkh.entity.vo.EventsVo1;
import com.hlingsoft.kkh.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 首页
 *
 * @author Herbert
 */

@RequestMapping("/v20")
@Controller
public class IndexAction_V20 extends BaseAction {


    private static final int rows = 10;


    /**
     * 首页接口
     *
     * @param longitude
     * @param latitude
     * @param request
     * @return
     */
    @RequestMapping(value = "/index.json")
    @ResponseBody
    public Map<String, Object> checkIn(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "longitude") Double longitude,
            @RequestParam(value = "latitude") Double latitude,
            @RequestParam(value = "columnId", defaultValue = "2", required = false) int columnId, //1.最新 2.主题 3.关注
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {
            AppUser checkUser = userDao.getUserById(userId);

            List<EventsVo1> eventsVo1List = new ArrayList<EventsVo1>();
            if (columnId == 2) {
                List<Events> eventsList = new ArrayList<Events>();
                if (userId != 0 && checkUser.getProfessionId()!=5) {
                    eventsList = eventsDao.getNewestEventsByAppUserId(userId, (pageNum - 1) * rows, rows);
                    if (eventsList.size()==0){
                       eventsList=eventsDao.getNewestEvents((pageNum - 1) * rows, rows);
                    }
                } else {
                    eventsList = eventsDao.getNewestEvents((pageNum - 1) * rows, rows);
                }

                for (Events events : eventsList) {

                    List<AppUser> collectUserList = new ArrayList<AppUser>();

                    EventsVo1 eventsVo1 = new EventsVo1();
                    //活动id
                    eventsVo1.setId(events.getId());
                    //活动名
                    eventsVo1.setName(events.getName());
                    //活动图片
                    EventComments firstEventComments = eventCommentsDao.getFirstEventComments(events.getId());
                    List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
                    eventsVo1.setImageList(imageList);
                    eventsVo1.setEventCommentsId(firstEventComments.getId());
                    //活动发布人信息
                    eventsVo1.setAppUserId(events.getAppUserId());
                    eventsVo1.setAppUser(userDao.getUserById(events.getAppUserId()));
                    //活动描述
                    eventsVo1.setDescription(events.getDescription());

                    //评论
                    List<Reply> replyList = replyDao.getReplyByEventCommentsId(firstEventComments.getId(), 0, 2);
                    Collections.sort(replyList, new Comparator<Reply>() {
                        @Override
                        public int compare(Reply o1, Reply o2) {
                            return o1.getCreateTime().compareTo(o2.getCreateTime());
                        }
                    });
                    eventsVo1.setReplyList(replyList);


                    //经纬度
                    eventsVo1.setLatitude(events.getLatitude());
                    eventsVo1.setLongitude(events.getLongitude());

                    //创建时间
                    eventsVo1.setCreateAddress(events.getCreateAddress());
                    eventsVo1.setCreateTime(events.getCreateTime());
                    eventsVo1.setUpdateTime(events.getUpdateTime());


                    //收藏人
                    List<Collect> collectList = collectDao.getByEventsId(events.getId(), 0, 10);
                    for (Collect collect : collectList) {
                        AppUser appUser = userDao.getUserById(collect.getAppUserId());
                        if (appUser != null) {
                            collectUserList.add(appUser);
                        }
                    }
                    eventsVo1.setCollectUserList(collectUserList);
                    eventsVo1.setCollectCount(collectDao.getByEventsIdCount(events.getId()));

                    //是否能收藏
                    if (userId != 0) {
                        if (collectDao.getByIds(userId, events.getId()) == null) {
                            eventsVo1.setIsCollect(0);
                        } else {
                            eventsVo1.setIsCollect(1);
                        }
                    } else {
                        eventsVo1.setIsCollect(0);
                    }

                    eventsVo1List.add(eventsVo1);
                }

                resultMap.put("eventsVo1List", eventsVo1List);
                resultMap.put("result", true);
            } else if (columnId == 1) {
                List<EventComments> eventCommentsList = eventCommentsDao.getNewestEventComments(rows * (pageNum - 1), rows);
                for (EventComments eventComments : eventCommentsList) {
                    //评论所属活动
                    Events events = eventsDao.getEventsById(eventComments.getEventId());
                    List<AppUser> likesUserList = new ArrayList<AppUser>();


                    EventsVo1 eventsVo1 = new EventsVo1();
                    //活动id
                    eventsVo1.setId(events.getId());
                    //活动名
                    eventsVo1.setName(events.getName());

                    //活动图片
                    List<Image> imageList = imageDao.getImageByEventCommentsId(eventComments.getId());
                    eventsVo1.setImageList(imageList);
                    //活动发布人信息
                    eventsVo1.setAppUserId(eventComments.getAppUserId());
                    eventsVo1.setAppUser(userDao.getUserById(eventComments.getAppUserId()));
                    //活动描述
                    eventsVo1.setDescription(eventComments.getContent());

                    //评论
                    List<Reply> replyList = replyDao.getReplyByEventCommentsId(eventComments.getId(), 0, 2);
                    for (Reply reply:replyList){
                        AppUser my = userDao.getUserById(reply.getMyId());
                        if(my!=null){
                            reply.setMyHeadImage(my.getHeadImage());
                        }
                    }
                    Collections.sort(replyList, new Comparator<Reply>() {
                        @Override
                        public int compare(Reply o1, Reply o2) {
                            return o1.getCreateTime().compareTo(o2.getCreateTime());
                        }
                    });
                    eventsVo1.setReplyList(replyList);

                    //经纬度
                    eventsVo1.setLatitude(events.getLatitude());
                    eventsVo1.setLongitude(events.getLongitude());
                    //对点跟帖的评论id
                    eventsVo1.setEventCommentsId(eventComments.getId());

                    //创建时间
                    eventsVo1.setCreateAddress(events.getCreateAddress());
                    eventsVo1.setCreateTime(events.getCreateTime());
                    eventsVo1.setUpdateTime(events.getUpdateTime());

                    //点赞人
                    List<Likes> likesList = likesDao.getLikesByEventCommentsId(eventComments.getId(), 0, 10);
                    for (Likes likes : likesList) {
                        AppUser appUser = userDao.getUserById(likes.getMyId());
                        if (appUser != null) {
                            likesUserList.add(appUser);
                        }
                    }
                    eventsVo1.setLikesUserList(likesUserList);
                    eventsVo1.setLikesCount(likesDao.getLikesByEventCommentsIdCount(eventComments.getId()));

                    //是否能点赞
                    if (userId != 0) {
                        if (likesDao.getCIsLikes(userId, eventComments.getId()).size() == 0) {
                            eventsVo1.setIsLike(0);
                        } else {
                            eventsVo1.setIsLike(1);
                        }
                    } else {
                        eventsVo1.setIsLike(0);
                    }

                    eventsVo1List.add(eventsVo1);
                }

                resultMap.put("eventsVo1List", eventsVo1List);
                resultMap.put("result", true);
            } else if (columnId == 3) {

                if (userId != 0) {
                    List<Events> eventsList = new ArrayList<Events>();
                    List<Focus> focusList = focusDao.getAttentions2ById(userId);
                    for (Focus focus : focusList) {
                        List<Events> eventsList1 = eventsDao.getEventsByAppUserId(focus.getAttentionsId(), 0, 10);
                        if (eventsList1.size() > 0) {
                            eventsList.addAll(eventsList1);
                        }
                    }
                    Collections.sort(eventsList, new Comparator<Events>() {
                        @Override
                        public int compare(Events o1, Events o2) {
                            return o1.getCreateTime().compareTo(o2.getCreateTime());
                        }
                    });

                    eventsList = Common.fenYe(eventsList, rows * (pageNum - 1), rows);
                    for (Events events : eventsList) {
                        List<AppUser> collectUserList = new ArrayList<AppUser>();

                        EventsVo1 eventsVo1 = new EventsVo1();
                        //活动id
                        eventsVo1.setId(events.getId());
                        //活动名
                        eventsVo1.setName(events.getName());
                        //活动图片
                        EventComments firstEventComments = eventCommentsDao.getFirstEventComments(events.getId());
                        List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
                        eventsVo1.setImageList(imageList);
                        eventsVo1.setEventCommentsId(firstEventComments.getId());
                        //活动发布人信息
                        eventsVo1.setAppUserId(events.getAppUserId());
                        eventsVo1.setAppUser(userDao.getUserById(events.getAppUserId()));
                        //活动描述
                        eventsVo1.setDescription(events.getDescription());

                        //评论
                        List<Reply> replyList = replyDao.getReplyByEventCommentsId(firstEventComments.getId(), 0, 2);
                        for (Reply reply:replyList){
                            AppUser my = userDao.getUserById(reply.getMyId());
                            if(my!=null){
                                reply.setMyHeadImage(my.getHeadImage());
                            }
                        }
                        Collections.sort(replyList, new Comparator<Reply>() {
                            @Override
                            public int compare(Reply o1, Reply o2) {
                                return o1.getCreateTime().compareTo(o2.getCreateTime());
                            }
                        });
                        eventsVo1.setReplyList(replyList);
                        //经纬度
                        eventsVo1.setLatitude(events.getLatitude());
                        eventsVo1.setLongitude(events.getLongitude());

                        //创建时间
                        eventsVo1.setCreateAddress(events.getCreateAddress());
                        eventsVo1.setCreateTime(events.getCreateTime());
                        eventsVo1.setUpdateTime(events.getUpdateTime());


                        if (userId != 0) {
                            if (likesDao.getIsLikes(userId, events.getId()).size() == 0) {
                                eventsVo1.setIsLike(0);
                            } else {
                                eventsVo1.setIsLike(1);
                            }
                        } else {
                            eventsVo1.setIsLike(1);
                        }

                        List<Collect> collectList = collectDao.getByEventsId(events.getId(), 0, 10);
                        for (Collect collect : collectList) {
                            AppUser appUser = userDao.getUserById(collect.getAppUserId());
                            if (appUser != null) {
                                collectUserList.add(appUser);
                            }
                        }
                        eventsVo1.setCollectUserList(collectUserList);
                        eventsVo1.setCollectCount(collectDao.getByEventsIdCount(events.getId()));

                        //是否能收藏
                        if (userId != 0) {
                            if (collectDao.getByIds(userId, events.getId()) == null) {
                                eventsVo1.setIsCollect(0);
                            } else {
                                eventsVo1.setIsCollect(1);
                            }
                        } else {
                            eventsVo1.setIsCollect(0);
                        }

                        eventsVo1List.add(eventsVo1);
                    }
                } else {
                    resultMap.put("msg", "登陆查看关注好友动态");
                }

                resultMap.put("eventsVo1List", eventsVo1List);
                resultMap.put("result", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 获取未读消息数
     *
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUnreadMessage.json")
    @ResponseBody
    public Map<String, Object> getUnreadMessage(
            @RequestParam(value = "userId") long userId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            int unreadCount = messageStatusDao.getUnreadChatCount(userId) + messageStatusDao.getUnreadNotificationCount(userId);
            resultMap.put("unreadCount", unreadCount);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 获取系统常量接口
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getConstant.json")
    @ResponseBody
    public Map<String, Object> getConstant(
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Interest> interestList = interestDao.getInterest();
            List<Profession> professionList = professionDao.getProfession();
            List<ReportRs> reportRsList = reportRsDao.getReportRs();
            resultMap.put("interestList", interestList);
            resultMap.put("professionList", professionList);
            resultMap.put("reportRsList", reportRsList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


}


